package com.itheima.mall.controller;

import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import com.itheima.mall.pojo.FlashPromotionProductRelation;
import com.itheima.mall.service.SmsFlashPromotionProductRelationService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flashProductRelation")
public class SmsFlashPromotionProductRelationController {

    @Autowired
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    /**
     * 批量选择商品添加关联
     */
    @PostMapping("/create")
    public ResponseEntity<String> create(@RequestBody List<FlashPromotionProductRelation> relationList){
        smsFlashPromotionProductRelationService.create(relationList);
        return ResponseEntity.ok("ok");
    }

    /**
     * 删除关联
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity<String> deleteRelation(@PathVariable Long id){
        smsFlashPromotionProductRelationService.deleteRelation(id);
        return ResponseEntity.ok("ok");
    }

    /**
     * 分页查询不同场次关联及商品信息
     */
    @GetMapping("/list")
    public ResponseEntity<PageResult> list(Integer flashPromotionId,Integer flashPromotionSessionId,
                                           Integer pageNum,Integer pageSize){
        PageResult result = smsFlashPromotionProductRelationService.list(flashPromotionId,flashPromotionSessionId,pageNum,pageSize);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改关联信息
     */
    @PostMapping("/update/{id}")
    public ResponseEntity<String> updateRelation(@PathVariable Long id,@RequestBody FlashPromotionProductRelation relation){
        smsFlashPromotionProductRelationService.updateRelation(id,relation);
        return ResponseEntity.ok("ok");
    }

    /**
     * 获取管理商品促销信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<FlashPromotionProductRelation> flashProductRelation(@PathVariable Long id){
        FlashPromotionProductRelation flashProductRelation = smsFlashPromotionProductRelationService.flashProductRelation(id);
        return ResponseEntity.ok(flashProductRelation);
    }


}
